package com.bosssoft.promotions.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bosssoft.base.entity.BaseEntity;
import com.bosssoft.promotions.dao.PromotionsDao;
import com.bosssoft.promotions.domain.dto.CouponQueryDTO;
import com.bosssoft.promotions.domain.dto.PromotionsDTO;
import com.bosssoft.promotions.domain.dto.PromotionsQueryDTO;
import com.bosssoft.promotions.domain.po.Coupon;
import com.bosssoft.promotions.domain.po.Promotions;
import com.bosssoft.promotions.service.PromotionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class PromotionsImpl implements PromotionsService {

    @Autowired
    private PromotionsDao promotionsDao;

    BaseEntity baseEntity = new BaseEntity();

    @Override
    public boolean addPromotion(PromotionsDTO promotionsDTO) {
        Date date = new Date();
        Promotions promotions = new Promotions();
        promotions.setPromotionId(promotionsDTO.getPromotionId());
        promotions.setPromotion(promotionsDTO.getPromotion());
        promotions.setBeginTime(promotionsDTO.getBeginTime());
        promotions.setEndTime(promotionsDTO.getEndTime());
        promotions.setCreateTime(date);
        promotions.setDescription(promotionsDTO.getDescription());
        promotions.setStatus("0");
        // 活动未删除
        promotions.setIsDelete("0");
        boolean justice = promotionsDao.insert(promotions)==1 ?  true: false;
        return justice;
    }

    @Override
    public boolean updatePromotion(PromotionsDTO promotionsDTO) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");

        UpdateWrapper<Promotions> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("promotion_id", promotionsDTO.getPromotionId());

        Promotions promotions = new Promotions();
        promotions.setStatus(promotionsDTO.getStatus());
        promotions.setUpdateTime(date);
        promotions.setIsDelete(promotionsDTO.getIsDelete());
        promotions.setDescription(promotionsDTO.getDescription());
        promotions.setBeginTime(promotionsDTO.getBeginTime());
        promotions.setEndTime(promotionsDTO.getEndTime());
        promotions.setPromotion(promotionsDTO.getPromotion());
        return promotionsDao.update(promotions, updateWrapper) == 1;
    }


    @Override
    public boolean deletePromotionById(PromotionsQueryDTO promotionsQueryDTO) {
        UpdateWrapper<Promotions> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("promotion_id",promotionsQueryDTO.getPromotionId()).set("is_delete", 1);

        return promotionsDao.update(null, updateWrapper)==1;
    }


    @Override
    public boolean batchDeletePromotionById(List<PromotionsQueryDTO> idList) {
        for(PromotionsQueryDTO promotionsQueryDTO:idList){
            UpdateWrapper<Promotions> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("promotion_id",promotionsQueryDTO.getPromotionId()).set("is_delete", 1);
            if(promotionsDao.update(null, updateWrapper)==0)
                return false;
        }
        return true;
    }


    @Override
    public Promotions queryPromotions(PromotionsQueryDTO promotionsQueryDTO) {
        return promotionsDao.selectById(promotionsQueryDTO.getPromotionId());
    }

    /*
     * 分页查询
     */
    @Override
    public Page<Promotions> QueryAllPagePromotions(int page, int size) {
        Page<Promotions> pages = new Page<>(page, size);
        return promotionsDao.selectPage(pages,new QueryWrapper<Promotions>());
    }
}
